package com.mankind.ssm.controller;

import com.mankind.ssm.common.CONSTANT;
import com.mankind.ssm.common.Page;
import com.mankind.ssm.pojo.Permission;
import com.mankind.ssm.pojo.Role;
import com.mankind.ssm.pojo.RolePermission;
import com.mankind.ssm.pojo.UserInfo;
import com.mankind.ssm.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Mankind on 2017/7/20.
 */
@Controller
@RequestMapping("/role")
public class RoleController {
    //service类
    @Autowired
    private UserService userService;
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private UserRoleService userRoleService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private RolePermissionService rolePermissionService;
    @Autowired
    private PermissionService permissionService;
    @ResponseBody
    @RequestMapping(value="/getRoleTotalPages",method= RequestMethod.POST,produces="application/json;charset=UTF-8")
    public Map getRoleTotalPages(HttpServletRequest request){
        Map<String,Object> json=new HashMap<String, Object>();
        //如果是带条件查询
        String text=request.getParameter("text");
        Integer totalPages=null;
        if(text==null||text==""){
            totalPages=roleService.getTotalNumber();
        }else{
            totalPages=roleService.getTotalNumber(text);
        }
        System.out.println("totalPages:"+totalPages);
        json.put("totalPages",totalPages);
        return json;
    }
    @ResponseBody
    @RequestMapping(value="/getRoleByPage",method= RequestMethod.POST,produces="application/json;charset=UTF-8")
    public Map getRoleByPage(HttpServletRequest request){
        Map<String,Object> json=new HashMap<String, Object>();
        Integer currentPage=Integer.parseInt(request.getParameter("Page"));
        String text=request.getParameter("text");
        Page<Role> page=null;
        if(text==null||text==""){
            page=roleService.getRoleByPage(CONSTANT.USERINFO_LIST_SIZE,currentPage);
        }else{
            page= roleService.fuzzyQueryByPage(currentPage,CONSTANT.USERINFO_LIST_SIZE,text);
        }
        json.put("roleList",page.getObj());
        return json;
    }
    @ResponseBody
    @RequestMapping(value="/loadRole",method= RequestMethod.POST,produces="application/json;charset=UTF-8")
    public Map loadRole(@RequestParam(required = true)Integer id){
        Map<String,Object> json=new HashMap<String, Object>();
        Role role=roleService.getRoleById(id);
        json.put("role",role);
        return json;
    }
    @RequestMapping(value="/createRole",method= RequestMethod.POST,produces="application/json;charset=UTF-8")
    public void createRole(Role role){
        roleService.insertRole(role);
    }
    @RequestMapping(value="/deleteRole",method= RequestMethod.POST,produces="application/json;charset=UTF-8")
    public void deleteRole(@RequestParam(required = true)Integer id){
        userRoleService.deleteByRoleId(id);
        rolePermissionService.deleteByRoleId(id);
        roleService.deleteByRoleId(id);
    }
    @RequestMapping(value="/updateRole",method= RequestMethod.POST,produces="application/json;charset=UTF-8")
    public void updateRole(Role role){
        roleService.updateRole(role);
    }
    @ResponseBody
    @RequestMapping(value="/getRoleAndPermissionByPage",method= RequestMethod.POST,produces="application/json;charset=UTF-8")
        public Map getRoleAndPermissionByPage(@RequestParam(required = true,value="Page")Integer currentPage,
                                              @RequestParam(required = false)String text){
        Map<String,Object> json=new HashMap<String, Object>();
        //如果是带条件查询
        Page<Role> page=null;
        if(text==null){
            page=roleService.getRoleByPage(CONSTANT.USERINFO_LIST_SIZE,currentPage);
        }else{
            page= roleService.fuzzyQueryByPage(currentPage,CONSTANT.USERINFO_LIST_SIZE,text);
        }
        int size=page.getObj().size();
        List<List<Permission>> permissionListOfRole=new ArrayList<List<Permission>>();
        List<Permission> allPermission=permissionService.selectAllPermission();
        //allRole.remove(0);
        for(int i=0;i<size;i++){
            Integer roleId=page.getObj().get(i).getRoleId();
            permissionListOfRole.add(rolePermissionService.getPermissionByRoleId(roleId));
        }
        json.put("roleList",page.getObj());
        json.put("permissionListOfRole",permissionListOfRole);
        json.put("allPermission",allPermission);
        return json;
    }
    @ResponseBody
    @RequestMapping(value="/allotPermission",method= RequestMethod.POST,produces="application/json;charset=UTF-8")
    public void allotPermission(@RequestParam(required = true,value="permission")String[] permissionIdList,
                                @RequestParam(required = true)Integer id){
        rolePermissionService.deleteByRoleId(id);
        for(int i=0;i<permissionIdList.length;i++){
            Integer permissionId=Integer.parseInt(permissionIdList[i]);
            rolePermissionService.insert(new RolePermission(id,permissionId));
        }
    }
}
